
# coding: utf-8

# # genHtml

# In[2]:


fileName = input("fileName:")


# In[3]:


modelName = input("modelName:")


# In[4]:


Input = "1"
Select = "2"
Date = "3"
Money = "4"
Rate = "5"
Area = "6"


# In[5]:


class HtmlTmpl:
    def __init__(self):
        self.headTmpl = """<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<script type="text/javascript">

</script>
<div class="view" id="form_{}">
    <input type="hidden" id="" name="" value="" />
    <fieldset>
        <legend></legend>
        <table class="tableStyle" formMode="transparent" >
"""
        self.footTmpl = """
        </table>
    </fieldset>
</div>
"""
        self.inputTmpl = """<input type="text" id="{}" name="{}" 
                        value='<s:property value="{}"/>' 
                        class="input_w180 validate[{}]" />"""
        self.selectTmpl = """<select id="{}" name="{}" 
                        selectedValue='<s:property value="{}"/>' 
                        prompt='<s:text name="label.select.default.value" />'
                        class="validate[{}]" selWidth="180" />"""
        self.dateTmpl = """<input type="text" id="{}"  name="{}" 
                        value='<s:date name="{}"  format="{}"/>'
                        class="date input_w180 validate[{}]" />"""
        self.moneyTmpl = """<input type="text" id="{}" name="{}"
                        value='<s:property value="{}"/>' intLength="12"
                        class="money input_w180_right validate[custom[illegalLetter]] validate[{}]" />"""
        self.rateTmpl = """<input type="text" id="{}" name="{}" 
                        value='<s:property value="{}"/>' 
                        class="input_w180_right validate[custom[moneyRate_2_4]] validate[{}]"
                        onblur="replenish4(this)" />"""
        self.areaTmpl = """<textarea style="width: 690px;" id="{}" name="{}" class="data validate[{}]" maxNum="200" >${}</textarea>"""
    #model为实体类名字 传入的arr格式为 [propName,propType,required]
    def genTmpl(self,model,arr):
        if len(arr) != 3:
            print("文件结束或输入文件格式错误!")
            return "<span></span>"
        propName = arr[0]
        propType = arr[1]
        required = arr[2]
        
        id = model + "_" + propName #id = model_propName
        name = model + "." + propName #name = model.propName
        value = model + "." + propName #value = {model.propName}
        if propType == Input:
            if required == "0":
                return self.inputTmpl.format(id,name,value,"")
            else:
                return self.inputTmpl.format(id,name,value,"required")
        elif propType == Select:
            if required == "0":
                return self.selectTmpl.format(id,name,value,"")
            else:
                return self.selectTmpl.format(id,name,value,"required")
        elif propType == Date:
            if required == "0":
                return self.dateTmpl.format(id,name,value,"%{getText('date.format')}","")
            else:
                return self.dateTmpl.format(id,name,value,"%{getText('date.format')}" ,"required")
        elif propType == Money:
            if required == "0":
                return self.moneyTmpl.format(id,name,value,"")
            else:
                return self.moneyTmpl.format(id,name,value,"required")
        elif propType == Rate:
            if required == "0":
                return self.rateTmpl.format(id,name,value,"")
            else:
                return self.rateTmpl.format(id,name,value,"required")
        elif propType == Area:
            if required == "0":
                return self.areaTmpl.format(id,name,"","{" + value + "}")
            else:
                return self.areaTmpl.format(id,name,"required","{" + value + "}")
        else:
            print("Unknown Type!")
            return -1


# In[6]:


f1 = open(fileName,"r",encoding='utf-8') #input file


# In[7]:


f2 = open(modelName + ".jsp","a+",encoding='utf-8') #output file


# In[8]:


tmpl = HtmlTmpl()


# In[9]:


f2.write(tmpl.headTmpl.format(modelName))


# In[10]:


tr = """            <tr>
                {0}
                {1}
            </tr>
"""


# In[11]:


td = """<td>
                    {}
                </td>
                <td>
                    {}
                </td>"""


# In[12]:


for line1 in f1:
    prop1,fmt1 = line1.split(':',1)
    td1Input = tmpl.genTmpl(modelName,fmt1.split())
    td1 = td.format(prop1,td1Input)
    try:
        line2 = f1.readline()
        prop2,fmt2 = line2.split(':',1)
        td2Input = tmpl.genTmpl(modelName,fmt2.split())
        td2 = td.format(prop2,td2Input)
    except (ValueError,IndexError):
        td2 = "" #最后一个节点可能为空
    finally:
        f2.write(tr.format(td1,td2))
    


# In[13]:


f2.write(tmpl.footTmpl)


# In[14]:


print("Success Creat JSP file:",modelName + ".jsp")


# In[15]:


f2.close()


# In[16]:


f1.close()

